using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Collections;

namespace DBConnectionLib
{
    public class DatabaseList
    {

        private string m_BaseDirectory;
        private string m_DatabaseListXMLFile;

        public string DatabaseListXMLFile
        {
            get { return m_DatabaseListXMLFile; }
            set { m_DatabaseListXMLFile = value; }
        }

        public string BaseDirectory
        {
            get { return m_BaseDirectory; }
            set { m_BaseDirectory = value; }
        }
        protected Hashtable m_DatabaseHashTable = new Hashtable();

        public DatabaseList()
        {
        }

        public DatabaseList(string strBaseDirectory)
        {
            this.m_BaseDirectory = strBaseDirectory;
        }

        public void Load(string strBaseDirectory, string strDatabaseListXml, bool bThrowExceptionIfNotFound)
        {
            this.m_DatabaseListXMLFile = strDatabaseListXml;
            this.m_BaseDirectory = strBaseDirectory;
            this.LoadData(bThrowExceptionIfNotFound);
        }

        public void LoadData(bool bThrowExceptionIfNotFound)
        {
            bool bFileFound = false;
            this.m_DatabaseHashTable.Clear();

            if (!System.IO.File.Exists(this.m_DatabaseListXMLFile))
            {
                this.m_DatabaseListXMLFile = this.BaseDirectory + "\\Databases.xml";
                if (!System.IO.File.Exists(this.m_DatabaseListXMLFile))
                {
                    if (bThrowExceptionIfNotFound)
                        throw new Exception(
                            String.Format("Can't locate Databases.xml file at \n{0}",
                            this.m_DatabaseListXMLFile));
                }
                else
                {
                    bFileFound = true;
                }

            }
            else
            {
                bFileFound = true;
            }

            if (bFileFound)
            {
                DataSets.DatabasesDS oDatabasesDS = new DataSets.DatabasesDS();
                oDatabasesDS.ReadXml(this.m_DatabaseListXMLFile);

                foreach (DataSets.DatabasesDS.DatabasesTableRow dbRow in
                    oDatabasesDS.DatabasesTable)
                {
                    Database newDb = new Database(dbRow.NickName, dbRow.ServerName, dbRow.DatabaseName, this.m_BaseDirectory);
                    if (!dbRow.IsConnectionStringNull())
                        newDb.ConnectionString = dbRow.ConnectionString;
                    if (!dbRow.IsUserNameNull())
                        newDb.UserName = dbRow.UserName;
                    if (!dbRow.IsPasswordNull())
                        newDb.Password = dbRow.Password;
                    if (!dbRow.IsCommandSQLNull())
                        newDb.CommandSQL = dbRow.CommandSQL;
                    if (!dbRow.IsSQLVersionNull())
                        newDb.SQLVersion = dbRow.SQLVersion;
                    this.AddDatabase(newDb);
                }
            }
        }
        public IEnumerable<Database> Databases
        {
            get
            {
                foreach (Object oObject in this.m_DatabaseHashTable.Values)
                    yield return (Database)oObject;
            }
        }
        public void ClearDatabases()
        {
            this.m_DatabaseHashTable.Clear();
        }

        public void AddDatabase(Database myDB)
        {
            if (!this.m_DatabaseHashTable.ContainsKey(myDB.NickName))
            {
                this.m_DatabaseHashTable.Add(myDB.NickName, myDB);
            }
        }

        public Database getDatabase(string strDbName)
        {
            if (m_DatabaseHashTable.ContainsKey(strDbName))
                return (Database)m_DatabaseHashTable[strDbName];
            else
                return null;
        }

        public void DeleteDatabase(string m_Name)
        {
            if (m_DatabaseHashTable.ContainsKey(m_Name))
                m_DatabaseHashTable.Remove(m_Name);
        }
        public void UpdateDatabase(string strName, string strServerName, string strDatabaseName)
        {
            Database oDatabase = (Database)m_DatabaseHashTable[strName];
            oDatabase.DataSourceName = strServerName;
            oDatabase.DatabaseName = strDatabaseName;
            m_DatabaseHashTable[strName] = oDatabase;
        }

        public void Save()
        {
            DataSets.DatabasesDS oDatabasesDS = new DataSets.DatabasesDS();
            foreach (Database oDatabase in this.Databases)
            {
                DataSets.DatabasesDS.DatabasesTableRow newRow = oDatabasesDS.DatabasesTable.NewDatabasesTableRow();
                newRow.DatabaseName = oDatabase.DatabaseName;
                newRow.ServerName = oDatabase.DataSourceName;
                newRow.NickName = oDatabase.NickName;
                newRow.ConnectionString = oDatabase.ConnectionString;
                newRow.CommandSQL = oDatabase.CommandSQL;
                newRow.UserName = oDatabase.UserName;
                newRow.Password = oDatabase.Password;

                oDatabasesDS.DatabasesTable.AddDatabasesTableRow(newRow);
            }

            if (!System.IO.Directory.Exists(this.BaseDirectory))
                System.IO.Directory.CreateDirectory(this.BaseDirectory);
            this.DatabaseListXMLFile = this.BaseDirectory + @"\Databases.xml";
            //FileUtils.EnsureFileIsWriteable(this.DatabaseListXMLFile);
            oDatabasesDS.WriteXml(this.DatabaseListXMLFile);
            
        }

        public int Count
        {
            get
            {
                return m_DatabaseHashTable.Count;
            }
        }
    }
}
